package com.dcxt.view;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.GestureDetector;

public class WebActivity extends Activity {
	private WebView mWebView;
	private DisplayMetrics dm;
	private float touchStartX;
	private float touchStartY;
	private Handler mHandler = new Handler();
	private GestureDetector detector;

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		dm = new DisplayMetrics();
		MyWebTouchListener touchListener = new MyWebTouchListener();
		detector = new GestureDetector(this, touchListener);

		getWindowManager().getDefaultDisplay().getMetrics(dm);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().setLayout(dm.widthPixels, dm.heightPixels);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.web_main);
		mWebView = (WebView) findViewById(R.id.webview);
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		// 此方法是在js中调用java代码
		mWebView.addJavascriptInterface(new Object() {
			public void clickOnAndroid() {
				mHandler.post(new Runnable() {
					public void run() {
						mWebView.loadUrl("javascript:wave()");
					}
				});
			}
		}, "demo");
		Bundle urlBundle = this.getIntent().getExtras();
		String url = urlBundle.getString("url");
		mWebView.loadUrl(url);
		mWebView.setWebChromeClient(new WebChromeClient() {

			public void onProgressChanged(WebView view, int progress) {
				setTitle("页面加载中，请稍候..." + progress + "%");
				setProgress(progress * 100);

				if (progress == 100) {
					setTitle(R.string.app_name);
				}
			}
		});
		// web
		mWebView.setWebViewClient(new MyWebViewClient());
		mWebView.setOnTouchListener(touchListener);

	}

	// 
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
			mWebView.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	// web
	public class MyWebViewClient extends WebViewClient {
		public boolean shouldOverviewUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}

	class MyWebTouchListener implements OnGestureListener, OnTouchListener {

		public boolean onDown(MotionEvent e) {
			// TODO Auto-generated method stub
			return false;
		}

		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if (Math.abs(e2.getX() - e1.getX()) < 10
					&& Math.abs(e2.getY() - e1.getY()) < 10) {
				return false;
			}
			final StringBuilder direction = new StringBuilder();

			touchStartX = e1.getX();
			touchStartY = e1.getY();

			float offsetX = e2.getX() - touchStartX;
			float offsetY = e2.getY() - touchStartY;
			// x方向的偏移量大于等于y方向的偏移量：方向肯定为左右
			if (Math.abs(offsetX) >= Math.abs(offsetY)) {
				if (offsetX > 0) {
					direction.append("left");
				} else if (offsetX < 0) {
					direction.append("right");
				}
			} else {
				if (offsetY > 0) {
					direction.append("down");
				} else if (offsetY < 0) {
					direction.append("up");
				}
			}
			// 调用js滑动事件代码
			// mHandler.post(new Runnable() {
			// public void run() {
//			String jsMethod = "javascript:test()";
			String jsMethod = "javascript:slideJS('" + direction.toString()
					+ "','" + touchStartX + "','" + touchStartY + "')";
			mWebView.loadUrl(jsMethod);
			// }
			// });
			return true;
		}

		public void onLongPress(MotionEvent e) {
			// TODO Auto-generated method stub

		}

		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {

			return false;
		}

		public void onShowPress(MotionEvent e) {
			// TODO Auto-generated method stub

		}

		public boolean onSingleTapUp(MotionEvent e) {
			// TODO Auto-generated method stub
			return false;
		}

		public boolean onTouch(View v, MotionEvent event) {
			return detector.onTouchEvent(event);
		}

	}
}
